package com.itboye.sunsun;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.custome.RoundImage;
import com.itboye.sunsun.person.ui.PersonSettingActivity;
import com.itboye.sunsun.person.ui.SttingActivity;
import com.itboye.sunsun.support.BaseFragment;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.utils.XImageLoader;
import com.itboye.sunsunhome.R;

@SuppressLint("ValidFragment")
public class MeFragment extends BaseFragment implements OnClickListener {

	private Context context;
	private View meView;

    private RoundImage  roundview;
    private TextView tvname;
    private RelativeLayout personsettingrela;

	public MeFragment() {
	}

	public MeFragment(Context context) {
		this.context = context;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		meView = inflater.inflate(R.layout.fragment_me, container, false);
		smartInject(meView);
		
		
		
		initMobile();
		
		
		// 注册广播
		IntentFilter intentFilterAvart = new IntentFilter("changeavart");
		getActivity().registerReceiver(avartBroadcastReceiver, intentFilterAvart);
		
		
		String uri=(String) SPUtils.get(App.ctx,null,SPContants.AVATAR_URL,"");
		XImageLoader.load(uri, roundview);
		return meView;
	}

	public void onClick(View v) {

		switch (v.getId()) {
//		case R.id.viewallrela:
//			
//	     	LoginController.onAllOrderClick(getActivity(), null);
//			break;
		case R.id.payrela:
			
			LoginController.onPayClick(getActivity(), null);
			
			break;
//		case R.id.sendrela:
//		
//			LoginController.onSendClick(getActivity(), null);
//			
//			break;
//		case R.id.receiverela:
//			
//			LoginController.onReceiveClick(getActivity(), null);
//			
//			break;
//		case R.id.customrela:
//			
//			LoginController.onCustomClick(getActivity(), null);
//			
//			break;
//		case R.id.mywealthrela:
//			
//			
//			LoginController.onMyWealthClick(getActivity(), null);
//			break;
		case R.id.mylocationrela:
			
			LoginController.onMyLocationClick(getActivity(), null);
			
			break;
		case R.id.mycollectrela:
			
			LoginController.onMyCollectClick(getActivity(), null);
			
			break;
		case R.id.personsettingrela:
			
//			LoginController.onPersonalsettingClick(getActivity(), null);
			Intent intent=new Intent(getActivity(),SttingActivity.class);
			startActivity(intent);
			Toast.makeText(getActivity(), "点了",0).show();
			
			break;
//		case R.id.signrela:
//			
//			LoginController.onSignClick(getActivity(), null);
//			
//			break;
//		case R.id.marketrela:
//			
//			LoginController.onPointMarketClick(getActivity(), null);
//			
//			break;
//		case R.id.jiangrela:
//			
//			LoginController.onJiangClick(getActivity(), null);
//			
//			break;
		case R.id.mywordsrela:
			
			LoginController.onMyWordsClick(getActivity(), null);
		
			break;
		case R.id.roundview:
			LoginController.onPersonalsettingClick(getActivity(), null);
			break;
			

		default:
			break;
		}

	}
	
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		// 注册广播
		IntentFilter intentFilterAvart = new IntentFilter(PersonSettingActivity.AVARTACTION);
		getActivity().registerReceiver(avartBroadcastReceiver, intentFilterAvart);
	    
		
		IntentFilter intentFilterExit = new IntentFilter(PersonSettingActivity.EXITCHANGE);
		getActivity().registerReceiver(avartBroadcastReceiver, intentFilterExit);
		
	};
	
	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			String resultData=(String) SPUtils.get(App.ctx, null, SPContants.AVATAR_URL,"");
			
			initMobile();
			XImageLoader.load(resultData, roundview);
		};
	};
	
	public void initMobile()
	{
		String phonenumber = (String) SPUtils.get(App.ctx, null, SPContants.NICK, "");
		tvname.setText(""+phonenumber);
		// Log.v("mobilephone", phonenumber+".... ");
//		if (!phonenumber.equals(""))
//
//		{
//			try
//			{
//			   String hintphone = phonenumber.substring(0, 3) + "****" + phonenumber.substring(7, 11);
//			   mobiletext.setText(hintphone);
//			}catch(Exception e)
//			{
//				e.printStackTrace();
//			}
//			
//		}else
//		{
//			mobiletext.setText(""+phonenumber);
//		}
	}

}
